package droid.frame.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.target.SimpleTarget;


/**
 * Created by 李丹丹 on 2016/11/1.
 */
public class GlideUtil {

    /**
     * 加载网络、本地图片,直接显示
     *
     * @param context     当前的上下文
     * @param url         图片的地址
     * @param placeholder 显示的默认图
     * @param imageView   需要显示的控件
     */
    public static void loadImage(Context context, String url, int placeholder, ImageView imageView) {
        if (placeholder == 0) {
            Glide.with(context)
                    .load(url)
                    .centerCrop()
                    .crossFade()
                    .skipMemoryCache(true)
                    .diskCacheStrategy(DiskCacheStrategy.RESULT)
                    .into(imageView);
        } else {
            Glide.with(context)
                    .load(url)
                    .placeholder(placeholder)
                    .centerCrop()
                    .crossFade()
                    .skipMemoryCache(true)
                    .diskCacheStrategy(DiskCacheStrategy.RESULT)
                    .into(imageView);
        }
    }

    public static void loadImage(Context context, String url, ColorDrawable placeholder, ImageView imageView) {
        Glide.with(context)
                .load(url)
                .placeholder(placeholder)
                .centerCrop()
                .crossFade()
                .skipMemoryCache(true)
                .diskCacheStrategy(DiskCacheStrategy.RESULT)
                .into(imageView);
    }

    public static void loadImage(Context context, String url, int placeholder, int width, int height, ImageView imageView) {
        Glide.with(context)
                .load(url)
                .placeholder(placeholder)
                .centerCrop()
                .crossFade()
                .skipMemoryCache(true)
                .override(width, height)
                .diskCacheStrategy(DiskCacheStrategy.RESULT)
                .into(imageView);
    }

    /**
     * 使用加载的图片的bitmap,到显示的控件中
     *
     * @param context      当前的上下文
     * @param url          图片的地址
     * @param placeholder  显示的默认图
     * @param simpleTarget 加载完bitmap的回调
     */
    public static void loadImage(Context context, String url, int placeholder, SimpleTarget simpleTarget) {
        Glide.with(context)
                .load(url)
                .asBitmap()
                .placeholder(placeholder)
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .skipMemoryCache(false)
                .into(simpleTarget);
    }

    /**
     * 使用加载的图片的bitmap,到显示的控件中
     *
     * @param context      当前的上下文
     * @param url          图片的地址
     * @param simpleTarget 加载完bitmap的回调
     */
    public static void loadImage(Context context, String url, ColorDrawable placeColor, SimpleTarget simpleTarget) {
        Glide.with(context)
                .load(url)
                .asBitmap()
                .placeholder(placeColor)
                .centerCrop()
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .skipMemoryCache(true)
                .into(simpleTarget);
    }

    public static void loadImage(Context context, String url, int width, int height, SimpleTarget<Bitmap> simpleTarget) {
        Glide.with(context)
                .load(url)
                .asBitmap()
                .centerCrop()
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .override(width, height)
                .skipMemoryCache(true)
                .into(simpleTarget);
    }

    public static void loadImageNoCrop(Context context, String url, ColorDrawable placeColor, ImageView imageView) {
        Glide.with(context)
                .load(url)
                .crossFade()
                .placeholder(placeColor)
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .skipMemoryCache(true)
                .into(imageView);
    }

    public static void loadImageNoCropImage(Context context, String url, int placeholder, ImageView imageView) {
        Glide.with(context)
                .load(url)
                .crossFade()
                .placeholder(placeholder)
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .skipMemoryCache(true)
                .into(imageView);
    }

    public static void loadImageNoCropST(Context context, String url, ColorDrawable placeColor, SimpleTarget<Bitmap> simpleTarget) {
        Glide.with(context)
                .load(url)
                .asBitmap()
                .placeholder(placeColor)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .skipMemoryCache(true)
                .into(simpleTarget);
    }

    /**
     * 使用加载的图片的bitmap,到显示的控件中
     *
     * @param context    当前的上下文
     * @param url        图片的地址
     * @param placeColor 占位的颜色
     * @param imageView  需要显示的控件
     */
    public static void loadImage(Context context, String url, ColorDrawable placeColor, ImageView imageView, boolean skipMemory) {
        Glide.with(context)
                .load(url)
                .crossFade()
                .placeholder(placeColor)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .skipMemoryCache(skipMemory)
                .centerCrop()
                .into(imageView);
    }

    public static void loadLocalImage(Context context, String url, ColorDrawable placeColor, ImageView imageView) {
        Glide.with(context)
                .load(url)
                .placeholder(placeColor)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .skipMemoryCache(true)
                .override(200, 200)
                .centerCrop()
                .into(imageView);
    }

    public static void loadBitmap(Context context, Uri url, SimpleTarget simpleTarget) {
        Glide.with(context)
                .load(url)
                .asBitmap()
                .into(simpleTarget);
    }

//    public static void loadAvatarWithTag(Context context, String tagUrl, ImageView imageView) {
//        loadAvatarWithTag(context, tagUrl, R.drawable.ic_launcher, imageView);
//    }

//    public static void loadAvatarWithTag(Context context, String tagUrl, int error, ImageView imageView) {
//        if (imageView.getTag(R.id.image_avatar_tag) == null || !imageView.getTag(R.id.image_avatar_tag).equals(tagUrl)) {
//            imageView.setTag(R.id.image_avatar_tag, tagUrl);
//            Glide.with(context)
//                    .load(tagUrl)
//                    .placeholder(R.drawable.ic_launcher)
//                    .error(error)
//                    .centerCrop()
//                    .crossFade()
//                    .skipMemoryCache(false)
//                    .diskCacheStrategy(DiskCacheStrategy.SOURCE)
//                    .into(imageView);
//        }
//    }

//    public static void loadAvatar(Context context, String tagUrl, int error, ImageView imageView) {
//        if (imageView.getTag(R.id.image_avatar_tag) == null || !imageView.getTag(R.id.image_avatar_tag).equals(tagUrl)) {
//            imageView.setTag(R.id.image_avatar_tag, tagUrl);
//            Glide.with(context)
//                    .load(tagUrl)
//                    .placeholder(R.drawable.ic_launcher)
//                    .error(error)
//                    .centerCrop()
//                    .skipMemoryCache(false)
//                    .diskCacheStrategy(DiskCacheStrategy.RESULT)
//                    .into(imageView);
//        }
//    }
}
